package org.zhang.shortlink.project.dao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.zhang.shortlink.project.dao.entity.LinkNetworkStatsDO;
import org.zhang.shortlink.project.dto.req.ShortLinkStatsReqDTO;

import java.util.List;

/**
 * @author zhang
 * @description 针对表【t_link_network_stats】的数据库操作Mapper
 * @createDate 2024-04-27 10:11:58
 * @Entity org.zhang.shortlink.project.dao.entity.LinkNetworkStatsDO
 */
@Mapper
public interface LinkNetworkStatsMapper extends BaseMapper<LinkNetworkStatsDO> {

    @Insert("INSERT INTO t_link_network_stats (id,full_short_url, date, cnt, network, create_time, update_time, del_flag) " +
            "VALUES(#{id}, #{fullShortUrl}, #{date}, #{cnt}, #{network},#{createTime},#{updateTime},#{delFlag}) " +
            "ON DUPLICATE KEY UPDATE cnt = cnt +  #{cnt};")
    void shortLinkNetworkState(LinkNetworkStatsDO linkNetworkStatsDO);

    /**
     * 根据短链接获取指定日期内访问网络监控数据
     */
    @Select("SELECT " +
            "    network, " +
            "    SUM(cnt) AS cnt " +
            "FROM " +
            "    t_link_network_stats " +
            "WHERE " +
            "    full_short_url = #{param.fullShortUrl} " +
            "    AND date BETWEEN #{param.startDate} and #{param.endDate} " +
            "GROUP BY " +
            "    full_short_url, network;")
    List<LinkNetworkStatsDO> listNetworkStatsByShortLink(@Param("param") ShortLinkStatsReqDTO requestParam);

}





